/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.OrdonanceTemplateDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.OrdonanceTemplateTBL;

/**
*
* @author Patrick kendzo
* Email: pkendzo@msn.com
*/

@Stateless
@Local({
    OrdonanceTemplateDAOInt.class
})
public class OrdonanceTemplateDAO implements OrdonanceTemplateDAOInt {

    @PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em;

    public OrdonanceTemplateDAO() {
    }

    @Override
    public void delete(Long id) {
        OrdonanceTemplateTBL ord = em.find(OrdonanceTemplateTBL.class, id);
        em.remove(ord);
        em.flush();
    }

    @Override
    public void persist(OrdonanceTemplateTBL todo) {
        em.persist(todo);
        em.flush();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<OrdonanceTemplateTBL> listOrdonanceTemplateBy_Typ(String typ) {
        try {

            List<OrdonanceTemplateTBL> resultList = (List<OrdonanceTemplateTBL>) em.createQuery(
                    "select o from OrdonanceTemplateTBL o where o.typ = '" + typ + "' order by label").getResultList();
            return resultList;
        } catch (Exception e) {
            throw new RuntimeException(
                    "Ordonance template for this typ can be found ", e);
        }
    }

    @Override
    public void update(OrdonanceTemplateTBL todo) {
        em.merge(todo);
        em.flush();

    }

    public OrdonanceTemplateTBL retrieveOrdonanceTemplateByID(Long id) {
        
       /* OrdonanceTemplateTBL result = (OrdonanceTemplateTBL)em.createQuery(
                    "select o from OrdonanceTemplateTBL o where o.id = '" + id + "'").getSingleResult();*/
        return (OrdonanceTemplateTBL) em.find(OrdonanceTemplateTBL.class, id);
    }
}
